//
// JavaPackageFinder
//
// Copyright (C) jextra.net.
//
//  This file is part of the Crack build system.
//
//  The Crack build system is free software; you can redistribute it 
//  and/or modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Crack build system is distributed in the hope that it will be 
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Crack build system; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.crack.tool;

import java.io.*;

/**
 * Given a java source file, reads the "package" that is specified in the file.
 */
public class JavaPackageFinder
{
    // ============================================================
    // Fields
    // ============================================================

    private File file;
    private String pkg;

    // ============================================================
    // Constructors
    // ============================================================

    public JavaPackageFinder()
    {
    }

    public JavaPackageFinder( File javaFile )
        throws IOException
    {
        read( javaFile );
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public void read( File file )
        throws IOException
    {
        this.file = file;
        BufferedReader reader = new BufferedReader( new FileReader( file ) );

        for ( String line = reader.readLine(); line != null; line = reader.readLine() )
        {
            line = line.trim();
            if ( line.startsWith( "package " ) || line.startsWith( "package\t" ) )
            {
                String[] strings = line.split( "[\\s;]" );
                pkg = strings[1];
                break;
            }
        }

        reader.close();
    }

    public String getPackage()
    {
        return pkg;
    }

    /**
     * Same as getPackage but with "." replaced with "/" and an extra "/" at the end.
     */
    public String getPath()
    {
        return pkg == null ? null : pkg.replace( '.', '/' ) + "/";
    }

    /**
     * Directory at the base of the package path. e.g. /xyz/abc/src/com/mycompany/component/File.java would have a root
     * directory of /xyz/abc/src.
     */
    public File getRootDir()
    {
        String string = getPackage() + ".";
        File dir = file.getParentFile();
        for ( int i = string.indexOf( '.' ); i >= 0; i = string.indexOf( '.', i + 1 ) )
        {
            dir = dir.getParentFile();
        }

        return dir;
    }
}
